<?php
/**
 * This file is part of the Yacms (http://www.yacms.org)
 * Copyright (c) 2019 gylr0505 (http://www.yacms.org)
 *
 * Author: 鬼一浪人 <gylr0505@163.com>
 *
 * DateTime: 19-10-27 下午2:31
 * Description:
 */
 
declare(strict_types = 1);

namespace App\Http\Controller;

use App\Model\Entity\Template;
use App\Template\Checker;
use App\Template\ParamsBag;
use App\Template\TemplateProvider;
use App\Template\TemplateSender;
use Swoft\Http\Message\Request;
use Swoft\Http\Server\Annotation\Mapping\Controller;
use Swoft\Http\Server\Annotation\Mapping\RequestMapping;


/**
 * Class TemplateController
 * @Controller()
 * @package App\Http\Controller
 */
class TemplateController
{
    /**
     * 创建模板配置信息
     * @RequestMapping(route="create")
     * @param Request $request
     */
    public function create(Request $request)
    {
    }

    /**
     * * * 模板列表信息
     * @RequestMapping(route="list")
     * @param Request $request
     * @return array
     * @throws \Swoft\Db\Exception\DbException
     */
    public function allList(Request $request)
    {
        $app_id = $request->post('app_id','');
        $type = $request->post('type','');
        $template_id = $request->post('template_id','');

        if (!$app_id){
            return [];
        }
        $where = ['app_id' => $app_id];
        if ($type){
            $where['type'] = $type;
        }
        if ($template_id){
            $where['template_id'] = $template_id;
        }

        return Template::query()->where($where)->get()->toArray();
    }

    /**
     * 子客户端推送数据到服务端
     * @RequestMapping(route="push")
     * @param Request $request
     * @return string
     * @throws \Swoft\Db\Exception\DbException
     */
    public function push(Request $request)
    {
        $app_id = $request->post('app_id','');
        $identity = $request->post('identity','');
        $open_id = $request->post('open_id','');
        if (!$app_id || !$identity || !$open_id){
            return 'fail';
        }

        $params  = Template::query()->where([
            'app_id' => $app_id,
            'identification' => $identity
        ])->first();

        if (!$params){
            return 'fail';
        }
        $provider = new TemplateProvider();
        $bool = $provider->generatorTemplate($params['id']);
        if(!$bool){
            return 'fail';
        }
        $params['open_id'] = $open_id;
        $provider->getTemplate()->push($params);
        return 'success';
    }

    /**
     * * 临时发送模板消息
     * @RequestMapping(route="send")
     * @param Request $request
     * @return string
     * @throws \ReflectionException
     * @throws \Swoft\Bean\Exception\ContainerException
     * @throws \Swoft\Db\Exception\DbException
     */
    public function send(Request $request)
    {
        try{
            $app_id = $request->post('app_id','');
            if (!$app_id){
                return 'fail';
            }
            $params = $request->getPost();

            $sender = new TemplateSender(new ParamsBag($params));
            $checker = new Checker($sender->execute());

            return $checker->isSuccess() ? 'success' : $checker->getErrorMsg();

        } catch (\Exception $e) {
            return 'fail';
        } catch (\Error $e) {
            return 'fail';
        }
    }

    /**
     *  * 停止某个消息队列的发送，会清空该模板消息的队列
     * @RequestMapping(route="stop")
     * @param Request $request
     * @return string
     * @throws \Swoft\Db\Exception\DbException
     */
    public function stopSend(Request $request)
    {
        $app_id = $request->post('app_id','');
        $identity = $request->post('identity','');
        if (!$app_id || !$identity){
            return 'fail';
        }
        $id = Template::query()->where([
            'app_id' => $app_id,
            'identification' => $identity
        ])->value('id');

        if (!$id){
            return 'fail';
        }
        $provider = new TemplateProvider();
        $provider->generatorTemplate($id);
        $provider->getTemplate()->clear();
        $provider = null;
        return 'success';
    }

    /**
     * * 模板消息开关
     * @RequestMapping(route="status")
     * @param Request $request
     * @return string
     * @throws \ReflectionException
     * @throws \Swoft\Bean\Exception\ContainerException
     * @throws \Swoft\Db\Exception\DbException
     */
    public function changeStatus(Request $request)
    {
        $app_id = $request->post('app_id','');
        $id = (int)$request->post('id',0);
        $status = (int)$request->post('status',0);
        if (!$app_id || !$id){
            return 'fail';
        }
        Template::query()->where([
            'id' => $id,
            'app_id' => $app_id
        ])->update([
            'status' => $status
        ]);
        return 'success';
    }
}